//
//  ModiSelfModel.m
//  Yihubang
//
//  Created by hyu on 2016/12/2.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "ModiSelfModel.h"
#import "TZImagePickerController.h"
#import "TZImageManager.h"
@interface ModiSelfModel()
@property (nonatomic,strong,readwrite)RACCommand *selectPhotoCommand;
@property (nonatomic,strong,readwrite)RACCommand *saveInfoCommend;
@property (nonatomic, strong, readwrite) RACCommand *updateAgainCommand;
@property (nonatomic,assign)BOOL changeHeadImage;
@property (nonatomic,strong)NSString *tipsStr;
@end
@implementation ModiSelfModel
- (void)initialize{
    [super initialize];
    @weakify(self)
    [[[RACObserve(self, updateInfo)
       filter:^(NSArray *events) {
           return @(events.count > 0).boolValue;
       }]
      deliverOnMainThread]
     subscribeNext:^(NSArray *events) {
         @strongify(self)
         [self.updateInfo removeObjectForKey:@"comment"];
         [self.updateInfo removeObjectForKey:@"inform_flag"];
         [self.updateInfo removeObjectForKey:@"uid"];
         [self.updateInfo setObject:self.updateInfo[@"img_top"] forKey:@"portrait"];
         [self.updateInfo setObject:self.updateInfo[@"nikename"] forKey:@"nickname"];
         [self.updateInfo removeObjectForKey:@"img_top"];
         [self.updateInfo removeObjectForKey:@"nikename"];
         NSMutableArray * info =[NSMutableArray array];
         NSMutableDictionary * nameDic =[NSMutableDictionary dictionary];
         [ nameDic setValue:self.updateInfo[@"nickname"] forKey:@"name"];
         [ nameDic setValue:self.updateInfo[@"portrait"] forKey:@"img_top"];
         [info addObject:nameDic];
         [info addObject:self.viewController.userClient.userName];
         [info addObject:self.updateInfo[@"sex"]];
         [info addObject:self.updateInfo[@"age"]];
         [info addObject:self.updateInfo[@"stature"]];
         [info addObject:self.updateInfo[@"shape"]];
         [info addObject:self.updateInfo[@"blood_group"]];
         [info addObject:self.updateInfo[@"anaphylaxis"]];
         [info addObject:self.updateInfo[@"special_medical_history"]];
     }];

    self.saveInfoCommend = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(UIImage * image) {
        @strongify(self)
        [self.viewController showHUDWithString:@""];
        if(self.changeHeadImage){
            [[[HttpManagerCenter sharedHttpManager] uploadImageOne:[image toUploadString] resultClass:nil] subscribeNext:^(BaseModel *model){
                @strongify(self)
                if(model.code==200){
                    [self.updateInfo setObject:model.data[@"url"] forKey:@"portrait"];
                    [self addOrUpdateFamily];
                }
            }];
        }else{
            [self addOrUpdateFamily];
        }
        return [RACSignal empty];
    }];
    self.updateAgainCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(UIImageView * input) {
        @strongify(self)
        //        self.updateidentificationImg =NO;
        [LYAuthorizedMaster albumAuthorityCheckSuccess:^{
            TZImagePickerController * imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
            [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
//                @strongify(self);
                UIImage * image = photos[0];
                input.image = image;
                self.changeHeadImage = YES;
//                [self.viewController showHUDWithString:@""];
//                [[[HttpManagerCenter sharedHttpManager] uploadImageOne:[image toUploadString]resultClass:nil] subscribeNext:^(BaseModel *model){
//                    @strongify(self);
//                    if(model.code==200){
//                        [self.updateInfo setObject:model.data[@"url"] forKey:@"portrait"];
//                        [self addOrUpdateFamily];
//                    }
//                }];
                
            }];
            [self.viewController presentViewController:imagePickerVc animated:YES completion:nil];
        } fail:^{
        }];
        
        return [RACSignal empty];
    }];
}

-(void) addOrUpdateFamily{
    //    [self.updateInfo setObject:@"儿子" forKey:@"relation"];
    if(![[self.updateInfo allKeys] containsObject:@"sex"]){
        [self.updateInfo setObject:@"1" forKey:@"sex"];
    }
    self.tipsStr = [[self.updateInfo objectForKey:@"sex"] isEqualToString:@"1"]?@"男神":@"女神";
    NSLog(@"%@",self.updateInfo);
    @weakify(self)
    [[[HttpManagerCenter sharedHttpManager] modifilrUserInfo:self.updateInfo resultClass:nil] subscribeNext:^(BaseModel *model){
        @strongify(self)
        if(model.code ==200){
            [self.updateInfo setObject:self.viewController.userClient.userInfo[@"identity"] forKey:@"identity"];
            [self.updateInfo setObject:self.viewController.userClient.userInfo[@"mobile"] forKey:@"mobile"];
            [self.viewController.userClient setUserInfo:self.updateInfo];
            [self.viewController hiddenHUDWithString:[NSString stringWithFormat:@"%@,您的信息已经完善成功了！",self.tipsStr] error:NO];
            [self performSelector:@selector(backCenter) withObject:nil afterDelay:2.0];
        }else{
            [self.viewController hiddenHUDWithString:model.message error:NO];
        }
    }];
}

-(void)backCenter{
    [self.viewController gotoBack];
}

-(NSMutableDictionary *)updateInfo{
    if(!_updateInfo){
        _updateInfo =[NSMutableDictionary dictionary];
    }
    return _updateInfo;
}

@end
